package com.tanhua.server.controller;

import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.server.service.SettingsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Fausto on 2021/9/27 19:50
 */
@RestController
@RequestMapping("/users")
public class SettingsController {

    @Autowired
    private SettingsService settingsService;

    /**
     * 查询通用设置
     */
    @GetMapping("/settings")
    public ResponseEntity settings(){
        SettingsVo vo=settingsService.settings();
        return ResponseEntity.ok(vo);
    }

    /**
     * 设置陌生人问题
     */
    @PostMapping("/questions")
    public ResponseEntity questions(@RequestBody Map map){
        //获取参数
        String content = (String) map.get("content");
        settingsService.saveQuestions(content);
        return ResponseEntity.ok(null);
    }

    /**
     * 通知设置
     */
    @PostMapping("/notifications/setting")
    public ResponseEntity notifications(@RequestBody Map map){
        //获取参数
        settingsService.saveSettings(map);
        return ResponseEntity.ok(null);
    }

    /**
     * 分页查询黑名单列表
     */
    @GetMapping("/blacklist")
    public ResponseEntity blacklist(@RequestParam(defaultValue = "1") int page,@RequestParam(defaultValue = "10") int size){
        //1.调用service查询
        PageResult pr=settingsService.blacklist(page,size);
        //2.构造返回
        return ResponseEntity.ok(pr);
    }

    /**
     * 取消黑名单
     */
    @DeleteMapping(value = "/blacklist/{uid}")
    public ResponseEntity deleteBlackList(@PathVariable("uid") Long blackUserId){
        settingsService.deleteBlackList(blackUserId);
        return ResponseEntity.ok(null);
    }

    /**
     * 1.修改手机号：发送验证码
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode(){
        settingsService.sendVerificationCode();
        return ResponseEntity.ok(null);
    }
    /**
     * 2.修改手机号：校验验证码
     */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map map){
        //1、调用map集合获取请求参数
        String code = (String) map.get("verificationCode");
        Boolean bl = settingsService.checkVerificationCode(code);
        Map<String,Object> result=new HashMap<>();
        result.put("verification", bl);
        return ResponseEntity.ok(result);
    }

    /**
     * 3.修改手机号：保存
     */
    @PostMapping("/phone")
    public ResponseEntity saveNewMobile(@RequestBody Map map){
        //1、调用map集合获取请求参数
        String mobile = (String) map.get("phone");
        settingsService.saveNewMobile(mobile);
        return ResponseEntity.ok(null);
    }

}
